<?php
	///model menampilkan
	class sword_model extends CI_Model{
		function long_sword($intid_dealer,$unit,$bulan){
			}
		function short_sword($intid_dealer,$strkode_dealer,$bulan){
			
		}
		//coba lagi
		function excalibur($dataTemp,$intid_dealer,$tglawal,$tglakhir){
			
		}
		function katana($dataTemp,$cekDealer){
			$omsetUnit = 0;
				$omset = 0;
				$var = "";
				for($i = 0;$i<sizeof($dataTemp);$i++){
					if($dataTemp[$i]['strkode_upline'] == $cekDealer){
						$omset		=	$dataTemp[$i]['inttotal_omset'];
						$var	=	$this->katana($dataTemp,$dataTemp[$i]['strkode_dealer']);
						$omsetUnit	=	$var + $omsetUnit + $omset;
						}
					}
				return $omsetUnit;
		}
		function flameberg($strkode_dealer,$bulan){
			$select = "select 
				(select sum(nota.inttotal_omset) from nota 
					where nota.intid_week in (select intid_week from week where intbulan = '".$bulan."') 
					and nota.intid_dealer = member.intid_dealer
					and nota.is_dp = 0
					and (nota.intid_jpenjualan = 1 or nota.intid_jpenjualan = 9)) inttotal_omset,
					member.strnama_dealer,
					member.strkode_dealer
				from member where member.intid_week in (select intid_week from week where intbulan = '".$bulan."') 
				and member.strkode_upline = '".$strkode_dealer."'";
			$query = $this->db->query($select);
			return $query->result();
		}
		/*
			Masamune (???), also known as Goro Nyudo Masamune (???????, Priest Goro Masamune, 
			c.1264–1343 AD),[1] is widely recognized as Japan's greatest swordsmith. 
			He created swords and daggers, known in Japanese as tachi and tanto respectively, in the Soshu tradition. 
			No exact dates are known for Masamune's life and he has reached an almost legendary status. 
			It is generally agreed that he made most of his swords in the late 13th and early 14th centuries, 1288–1328. 
			Some stories list his family name as Okazaki, 
			but some experts believe this is a fabrication to enhance the standing of the Tokugawa family.[2]
		*/
		function HonjoMasamune($intid_dealer,$tglawal,$tglakhir){
			$data	= array();
			$var	= array();
			$temp	= array();
			$query = $this->db->query('select
			 		strkode_dealer,
					intid_dealer,
					upper(strnama_dealer) strnama_dealer,
					strkode_upline,
					intlevel_dealer,
					intparent_leveldealer,
					intid_unit,
					(select sum(nota.inttotal_omset) 
						from nota 
							where nota.datetgl >= "'.$tglawal.'" and nota.datetgl <= "'.$tglakhir.'"
							and nota.intid_dealer = "'.$intid_dealer.'" and nota.is_dp = 0 
							and (nota.intid_jpenjualan = 1 or nota.intid_jpenjualan = 9)
							) inttotal_omset
				from member
				where intid_dealer = "'.$intid_dealer.'"');
			foreach($query->result() as $row){
				//code in here
				$data[] = array('strkode_dealer' => $row->strkode_dealer,
						'intid_dealer' => $row->intid_dealer,
						'strnama_dealer' => $row->strnama_dealer,
						'strkode_upline' => $row->strkode_upline,
						'intlevel_dealer' => $row->intlevel_dealer,
						'intparent_leveldealer' => $row->intparent_leveldealer,
						'inttotal_omset' => $row->inttotal_omset,
						'intid_unit'	=> $row->intid_unit,
					);
				$temp = $this->SwordOfGoujian($row->strkode_dealer,$row->intid_dealer,$tglawal,$tglakhir,$row->intid_unit);
				for($i=0;$i<sizeof($temp);$i++){
					if(isset($temp[$i]['strkode_dealer'])){
						$var[0]	=	$temp[$i]['strkode_dealer'];
					}
					if(isset($temp[$i]['intid_dealer'])){
						$var[1]	=	$temp[$i]['intid_dealer'];
					}
					if(isset($temp[$i]['strnama_dealer'])){
						$var[2]	=	$temp[$i]['strnama_dealer'];
					}
					if(isset($temp[$i]['strkode_upline'])){
						$var[3]	=	$temp[$i]['strkode_upline'];
					}
					if(isset($temp[$i]['intlevel_dealer'])){
						$var[4]	=	$temp[$i]['intlevel_dealer'];
					}
					if(isset($temp[$i]['intparent_leveldealer'])){
						$var[5]	=	$temp[$i]['intparent_leveldealer'];
					}
					if(isset($temp[$i]['inttotal_omset'])){
						$var[6]	=	$temp[$i]['inttotal_omset'];
					}
					if(isset($temp[$i]['intid_unit'])){
						$var[7]	=	$temp[$i]['intid_unit'];
					}
					$data[] = array('strkode_dealer' => $var[0],
							'intid_dealer' => $var[1],
							'strnama_dealer' => $var[2],
							'strkode_upline' => $var[3],
							'intlevel_dealer' => $var[4],
							'intparent_leveldealer' => $var[5],
							'inttotal_omset' => $var[6],
							'intid_unit'	=> $var[7],
						);
				}
			}
			return $data;
		}
		
		/*
			The Sword of Goujian is an archaeological artifact of the Spring 
			and Autumn Period (771 to 403 BCE) found in 1965 in Hubei, China. 
			Renowned for its sharpness and resistance to tarnish, 
			this historical artifact of ancient China is currently in the possession of the Hubei Provincial Museum.
		*/
		function SwordOfGoujian($strkode_dealer,$intid_dealer,$tglawal,$tglakhir,$intid_unit){
			$data = array();
			$temp = array();
			$var	= array();
			$select = 'select 
					member.strkode_dealer,
					member.intid_dealer,
					upper(member.strnama_dealer) strnama_dealer,
					member.strkode_upline,
					member.intlevel_dealer,
					member.intparent_leveldealer,
					member.intid_unit,
					(select if(
							(select sum(inttotal_omset) from nota 
								where nota.datetgl >= "'.$tglawal.'" and nota.datetgl <= "'.$tglakhir.'" 
								and nota.is_dp = 0 and (nota.intid_jpenjualan = 1 or nota.intid_jpenjualan = 9)
								and nota.intid_dealer = member.intid_dealer
								) > 0,
								 (select sum(inttotal_omset) from nota 
									where nota.datetgl >= "'.$tglawal.'" and nota.datetgl <= "'.$tglakhir.'" 
									and nota.is_dp = 0 and (nota.intid_jpenjualan = 1 or nota.intid_jpenjualan = 9)
									and nota.intid_dealer = member.intid_dealer), 
									0)
								)inttotal_omset
					from member 
					where 
						member.strkode_upline="'.$strkode_dealer.'" 
						and member.intlevel_dealer != 1
						and member.intid_unit = "'.$intid_unit.'"
						group by member.strkode_dealer order by member.strkode_dealer asc';
			$Query = $this->db->query($select);
			foreach($Query->result() as $row){
				$data[] = array('strkode_dealer' => $row->strkode_dealer,
						'intid_dealer' => $row->intid_dealer,
						'strnama_dealer' => $row->strnama_dealer,
						'strkode_upline' => $row->strkode_upline,
						'intlevel_dealer' => $row->intlevel_dealer,
						'intparent_leveldealer' => $row->intparent_leveldealer,
						'inttotal_omset' => $row->inttotal_omset,
						'intid_unit'	=> $row->intid_unit,
					);
				
				$temp = $this->SwordOfGoujian($row->strkode_dealer,$row->intid_dealer,$tglawal,$tglakhir,$intid_unit);
				for($i=0;$i<sizeof($temp);$i++){
					if(isset($temp[$i]['strkode_dealer'])){
						$var[0]	=	$temp[$i]['strkode_dealer'];
					}
					 if(isset($temp[$i]['intid_dealer'])){
						$var[1]	=	$temp[$i]['intid_dealer'];
					}
					if(isset($temp[$i]['strnama_dealer'])){
						$var[2]	=	$temp[$i]['strnama_dealer'];
					}
					if(isset($temp[$i]['strkode_upline'])){
						$var[3]	=	$temp[$i]['strkode_upline'];
					}
					if(isset($temp[$i]['intlevel_dealer'])){
						$var[4]	=	$temp[$i]['intlevel_dealer'];
					}
					if(isset($temp[$i]['intparent_leveldealer'])){
						$var[5]	=	$temp[$i]['intparent_leveldealer'];
					}
					if(isset($temp[$i]['inttotal_omset'])){
						$var[6]	=	$temp[$i]['inttotal_omset'];
					}
					if(isset($temp[$i]['intid_unit'])){
						$var[7]	=	$temp[$i]['intid_unit'];
					}
					$data[] = array('strkode_dealer' => $var[0],
							'intid_dealer' => $var[1],
							'strnama_dealer' => $var[2],
							'strkode_upline' => $var[3],
							'intlevel_dealer' => $var[4],
							'intparent_leveldealer' => $var[5],
							'inttotal_omset' => $var[6],
							'intid_unit'	=> $var[7]
						);
				}
			}
			return $data;
		}
	}
?>